<?php


namespace app\admin\controller;

use app\admin\model\CustomerModel;
use app\admin\model\FenPeiModel;
use app\admin\service\CustomerService;
use app\admin\validate\CustmoterValidate;
use app\extra\lib\exception\CustomerException;

/**
 * Class Customer
 * @package app\admin\controller
 * 客户管理模块下的功能模块
 */
class Customer extends Main{

    /**
     * 跳转到客户分配信息
     */
    public function goFenPei(){
        return $this->fetch("match_customer");
    }

    /**
     * 跳转到客户未分配信息
     */
    public function goWeiFenPei(){
        return $this->fetch("wei_match_customer");
    }

    /**
     * 获取客户分配列表信息
     */
    public function matchCustomer(){
        $page = isset($_GET['page'])?$_GET['page']:1;
        $size = isset($_GET['size'])?$_GET['size']:10;
        //参数校验
        (new CustmoterValidate())->goCheck();
        $customerService = new CustomerService();
        $customers = $customerService->getCustmoers($page,$size);
        $this->assign('customers', $customers);

        if(!$customers){
            throw new CustomerException();
        }
        $result = [];
        $result["code"] = 0;
        $result["msg"] = "";
        $result["count"] = $customerService->getCount();
        $result["data"] = $customers;
        return json($result);
    }

    /**
     * 获取所有的管理员
     */
    public function getGuanLiYuanAll(){
        $id = isset($_GET['id'])?$_GET['id']:"";
        $gly_id = isset($_GET['gly_id'])?$_GET['gly_id']:"";
        $customerService = new CustomerService();
        $guanLiYuans = $customerService->getGuanLiYuanAll();
        $this->assign('guanLiYuans', $guanLiYuans);
        $this->assign('id', $id);
        $this->assign('gly_id', $gly_id);
        return $this->fetch("guan_li_yuan_edit");
    }

    /**
     * 获取所有的管加
     */
    public function getGuanJiaAll(){
        $id = isset($_GET['id'])?$_GET['id']:"";
        $gj_id = isset($_GET['gj_id'])?$_GET['gj_id']:"";
        $customerService = new CustomerService();
        $guanJias = $customerService->getGuanJiaAll();
        $this->assign('guanJias', $guanJias);
        $this->assign('id', $id);
        $this->assign('gj_id', $gj_id);
        return $this->fetch("guan_jia_edit");
    }

    //更新管家分配
    public function updateGaunJia(){
        $user_id = isset($_POST['id'])?$_POST['id']:"";
        $guan_jia_id = isset($_POST['guan_jia_id'])?$_POST['guan_jia_id']:"";
        if($user_id!=""&&$guan_jia_id!=""){
// save方法第二个参数为更新条件
            FenPeiModel::update([
                'guan_jia_id'  => $guan_jia_id
            ],['user_id' => $user_id],['guan_jia_id']);
            $result = [];
            $result["code"] = 200;
            $result["msg"] = "更新成功";
            $result["data"] = "更新成功";
            return json($result);
        }else{
            $result = [];
            $result["code"] = 0;
            $result["msg"] = "更新失败";
            $result["data"] = "更新失败";
            return json($result);
        }
    }

    //更新管理员分配
    public function updateGaunLiYuan(){
        $user_id = isset($_POST['id'])?$_POST['id']:"";
        $guan_li_yuan_id = isset($_POST['guan_li_yuan_id'])?$_POST['guan_li_yuan_id']:"";
        if($user_id!=""&&$guan_li_yuan_id!=""){
            // save方法第二个参数为更新条件
            FenPeiModel::update([
                'admin_id'  => $guan_li_yuan_id
            ],['user_id' => $user_id],['admin_id']);
            $result = [];
            $result["code"] = 200;
            $result["msg"] = "更新成功";
            $result["data"] = "更新成功";
            return json($result);
        }else{
            $result = [];
            $result["code"] = 0;
            $result["msg"] = "更新失败";
            $result["data"] = "更新失败";
            return json($result);
        }
    }

    /**
     * 查询未分配的用户
     */
    public function getWeiFenPeiUsers(){
        $page = isset($_GET['page'])?$_GET['page']:1;
        $size = isset($_GET['size'])?$_GET['size']:10;
        $customerService = new CustomerService();
        $users =$customerService->getWeiFenPeiUsers($page,$size) ;
        $result = [];
        if($users){
            $result["code"] = 0;
            $result["msg"] = "获取成功";
            $result["data"] =$users;

        }else{
            $result["code"] = 0;
            $result["msg"] = "获取失败";
            $result["data"] = "";

        }
        return json($result);
    }

}